﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Lk.WebTow.Models;
using Lk.WebTow.Filter;
using Lk.WebTow.Authorize;
using NLog;
using Microsoft.Extensions.Logging;
using Lk.WebTow.Common.Cache;

namespace Lk.WebTow.Controllers
{

    [UserAuthorize]
    public class HomeController : Controller
    {

        private readonly ILogger<HomeController> logger;
        public HomeController(ILogger<HomeController> logger)
        {
            this.logger = logger;
        }
        public IActionResult Index()
        {
            var userClaims = User.Claims;

            ViewData["UserName"] = userClaims.FirstOrDefault(s => s.Type == "UserName")?.Value;
            ViewData["UserId"] = userClaims.FirstOrDefault(s => s.Type == "UserId")?.Value;
            var rootDirectory = AppContext.BaseDirectory;


            logger.LogInformation("你访问了首页");
            logger.LogWarning("警告信息");
            logger.LogError("错误信息");

            #region redis

            RedisHelper.Default.SetStringKey("test", "1234");

            var str = RedisHelper.Default.GetStringKey("test");
            #endregion
            return View();
        }

        public IActionResult About()
        {
            //var config = _appConfigurtaionServices.AppConfigurations.FileUpPath;
            //ViewData["Message"] = config;
            return View();
        }


        public IActionResult Test(string v)
        {
            try
            {
                // int s = int.Parse(v);
            }
            catch (Exception)
            {
                return new StatusCodeResult(429);
            }
            return Content(v);
        }
        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        [Route("error/404")]
        public IActionResult Error404()
        {
            return View();
        }

        [Route("error/{code:int}")]
        public IActionResult Error(int code)
        {
            // handle different codes or just return the default error view
            return View();
        }
    }
}
